<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script type="text/javascript">
    require([ "dojo/_base/array", "dojo/_base/xhr", "dojo/request", "dojo/topic", "dojo/json" ], function(array, xhr,
            request, topic, JSON) {
        var deletItemsCount = 0;
        syspermissionRestForm = function(formId) {
            dijit.byId(formId).reset();
            if (formId.indexOf('UFD') != -1) {
                var rowid = jQuery("#syspermissionGird").jqGrid('getGridParam', 'selrow');
                var sysuser = jQuery("#syspermissionGird").jqGrid('getRowData', rowid);
                dijit.byId('syspermissionUFD').setValues(sysuser);
            }
        };
        topic.subscribe("syspermission/insertOrUpdate", syspermissionRestForm);
        var syncDB = function(url, jsonData) {
            request.post(url, {
                data : jsonData,
                headers : {
                    'Content-Type' : "application/json; charset=utf-8"
                },
                timeout : 30000
            }).then(function(response) {
                topic.publish("notifyMsgTopic", {
                    message : response,
                    type : "message",
                    duration : 1000
                });
                var currentPage = jQuery('#syspermissionGird').jqGrid('getGridParam', 'page');
                var pageSize = jQuery('#syspermissionGird').jqGrid('getGridParam', 'rowNum');
                var totalCount = jQuery('#syspermissionGird').jqGrid('getGridParam', 'records') - deletItemsCount;
                var totalPages = Math.ceil(totalCount / pageSize);
                jQuery("#syspermissionGird").jqGrid('setGridParam', {
                    page : currentPage > totalPages ? totalPages : currentPage
                }).trigger("reloadGrid");
            }, function(error) {
                alert(error);
            });
        };
        syspermissionSubmitForm = function(formId, type) {
            var url = "syspermission/insert";
            if (type == 'u') {
                url = "syspermission/update";
            }
            var form = dijit.byId(formId);
            if (form.isValid()) {
                var json = JSON.stringify(form.getValues());
                syncDB(url, json);
                form.hide();
                topic.publish("syspermission/insertOrUpdate", formId);
            } else {
                form.validate();
            }
        };
        syspermissionDelete = function() {
            var ids = jQuery("#syspermissionGird").jqGrid('getGridParam', 'selarrrow') || [];
            deletItemsCount = ids.length;
            if (ids.length > 0) {
                if (confirm('确认删除所选定的项？')) {
                    var syspermission = {};
                    syspermission.id = '';
                    array.forEach(ids, function(entry, i) {
                        syspermission.id += entry + ",";
                    });
                    syncDB("syspermission/delete", JSON.stringify(syspermission));
                }
            } else {
                alert('请选择至少一条数据!');
            }
        };
        syspermissionQuery = function(formId) {
            var syspermission = dijit.byId(formId).getValues();
            jQuery("#syspermissionGird").jqGrid('setGridParam', {
                postData : syspermission
            }).trigger('reloadGrid');
        };
    });
</script>
<div data-dojo-type="dijit/Dialog" id="syspermissionIFD" title="新增">
  <div class="dijitDialogPaneContentArea">
    <table>
      <tr>
        <td><label for="permitName">权限名称: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="permitName"></td>
      </tr>
      <tr>
        <td><label for="description">描述: </label></td>
        <td><input data-dojo-type="dijit/form/Textarea" data-dojo-props="trim:true" type="textarea"
          name="description"></td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="syspermissionSubmitForm('syspermissionIFD','i')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="syspermissionRestForm('syspermissionIFD');">重置</button>
  </div>
</div>
<div data-dojo-type="dijit/Dialog" id="syspermissionUFD" title="更新">
  <div class="dijitDialogPaneContentArea">
    <input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, readOnly: true" type="text"
      name="permitId" style="display: none;">
    <table>
      <tr>
        <td><label for="permitName">权限名称: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="permitName"></td>
      </tr>
      <tr>
        <td><label for="description">描述: </label></td>
        <td><input data-dojo-type="dijit/form/Textarea" data-dojo-props="trim:true" type="textarea"
          name="description"></td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="syspermissionSubmitForm('syspermissionUFD','u')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="syspermissionRestForm('syspermissionUFD');">重置</button>
  </div>
</div>
<fieldset>
  <legend>&nbsp;&nbsp;信息选项&nbsp;&nbsp;</legend>
  <form id="querySysPermissionForm" data-dojo-type="dijit/form/Form" method="post">
    <div style="margin-left: 5px;">
      <table>
        <tr>
          <td><label for="permitName">权限名称: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="permitName"></td>
        </tr>
        <tr>
          <td><label for="description">描述: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text"
            name="description"></td>
        </tr>
      </table>
    </div>
  </form>
  <div style="padding: 5px;">
    <button id="syspermissionInsertBtn" data-dojo-type="dijit/form/Button" type="submit" label="新增">
      <script type="dojo/method" event="onClick" args="evt">
        dijit.byId('syspermissionIFD').show();
      </script>
    </button>
    <button id="syspermissionDeleteBtn" data-dojo-type="dijit/form/Button" type="button" label="删除"
      onclick="syspermissionDelete();"></button>
    <button id="syspermissionUpdateBtn" data-dojo-type="dijit/form/Button" type="button" label="更新">
      <script type="dojo/method" event="onClick" args="evt">
        var ids = jQuery("#syspermissionGird").jqGrid('getGridParam', 'selarrrow') || [];
        if (ids.length != 1) {
          alert('每次只能修改一条数据!');
        }else{
          var syspermission = jQuery("#syspermissionGird").jqGrid('getRowData', ids[0]);
          dijit.byId('syspermissionUFD').setValues(syspermission);
          dijit.byId('syspermissionUFD').show();
        }
      </script>
    </button>
    <button id="syspermissionQueryBtn" data-dojo-type="dijit/form/Button" type="button" label="查询"
      onclick="syspermissionQuery('querySysPermissionForm');"></button>
  </div>
</fieldset>
<div id="gird">
  <table id="syspermissionGird"></table>
  <div id="syspermissionToolbar"></div>
</div>
<script type="text/javascript">
    jQuery("#syspermissionGird").jqGrid({
        url : 'syspermission/queryAsList',
        datatype : "json",
        autowidth : true,
        rownumbers : true,
        height : 'auto',
        colNames : [ '主键', '权限名称', '描述' ],
        colModel : [ {
            name : 'permitId',
            index : 'permitId',
            width : 60,
            key : true,
            hidden : true,
            align : 'center'
        }, {
            name : 'permitName',
            index : 'permitName'
        }, {
            name : 'description',
            index : 'description'
        } ],
        rowNum : 15,
        rowList : [ 15, 20, 25, 30 ],
        pager : '#syspermissionToolbar',
        // sortname : 'username',
        recordpos : 'right',
        viewrecords : true,
        sortorder : "desc",
        jsonReader : {
            repeatitems : false,
            id : "0"
        },
        multiselect : true,
        shrinkToFit : false,
        caption : "权限列表"
    });
    jQuery("#syspermissionGird").jqGrid('navGrid', '#syspermissionToolbar', {
        edit : false,
        add : false,
        del : false,
        search : false,
        refresh : true
    });
</script>
